package com.cytangaa.youtubedownload;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;

import android.os.Environment;
import android.util.Log;

public class DownloadYoutube {
	public DownloadYoutube(){
		
	}
	
	public void DownloadNow(String url, String name){
		URL u = null;
		InputStream is = null;  

		     try {
		              u = new URL(url);
		              is = u.openStream(); 
		              HttpURLConnection huc = (HttpURLConnection)u.openConnection();//to know the size of video
		              int size = huc.getContentLength();                 

		          if(huc != null){
		              String fileName = name;
		              String storagePath = Environment.getExternalStorageDirectory().toString();
		              //String storagePath = "";
		              File f = new File(storagePath,fileName);

		              FileOutputStream fos = new FileOutputStream(f);
		              byte[] buffer = new byte[1024];
		              int len1 = 0;
		              if(is != null){
		                 while ((len1 = is.read(buffer)) > 0) {
		                       fos.write(buffer,0, len1);   
		                 }
		              }
		              if(fos != null){
		                 fos.close();
		              }
		          }                     
		     }catch (MalformedURLException mue) {
		            mue.printStackTrace();
		     } catch (IOException ioe) {
		            ioe.printStackTrace();
		    } finally {
		               try {                
		                 if(is != null){
		                   is.close();
		                 }
		               }catch (IOException ioe) {
		                     // just going to ignore this one
		               }
		    }
	}
	
	public void DownloadFromUrl(String DownloadUrl, String fileName) {
		Log.i("DownloadFromUrl","DownloadFromUrl"+DownloadUrl);

		   try {
		           File root = android.os.Environment.getExternalStorageDirectory();               

		           File dir = new File (root.getAbsolutePath() + "/youlike");
		           if(dir.exists()==false) {
		                dir.mkdirs();
		           }

		           URL url = new URL(DownloadUrl); //you can write here any link
		           File file = new File(dir, fileName);

		           long startTime = System.currentTimeMillis();
		           Log.d("DownloadManager", "download begining");
		           Log.d("DownloadManager", "download url:" + url);
		           Log.d("DownloadManager", "downloaded file name:" + fileName);
		           

		           /* Open a connection to that URL. */
		           URLConnection ucon = url.openConnection();
		           Log.i("URLConnection","URLConnection Succeed");

		           /*
		            * Define InputStreams to read from the URLConnection.
		            */ 
		           InputStream is = ucon.getInputStream();
		           BufferedInputStream bis = new BufferedInputStream(is);
		           Log.i("BufferedInputStream","BufferedInputStream Succeed");

		           /*
		            * Read bytes to the Buffer until there is nothing more to read(-1).
		            */
		           ByteArrayBuffer baf = new ByteArrayBuffer(5000);
		           int current = 0;
		           while ((current = bis.read()) != -1) {
		              baf.append((byte) current);
			          Log.i("ByteArrayBuffer","ByteArrayBuffer Succeed");

		           }


		           /* Convert the Bytes read to a String. */
		           FileOutputStream fos = new FileOutputStream(file);
			       Log.i("FileOutputStream","FileOutputStream initialized");
		           fos.write(baf.toByteArray());
			       Log.i("fos.write(baf.toByteArray());","fos.write(baf.toByteArray()); initialized");
		           fos.flush();
		           fos.close();
		           Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");

		   } catch (IOException e) {
		       Log.d("DownloadManager", "Error: " + e);
		   }

		}

}
